#include "netcardutil.h"
#include "rtc6/RTC6impl.hpp"

NetCardUtil::NetCardUtil(int cardNum, QObject *parent) : QThread(parent), m_card_num(cardNum)
{

}

NetCardUtil::~NetCardUtil()
{
    m_running = false;
    QThread::msleep(100);
}

void NetCardUtil::run()
{
    m_running = true;
    while(m_running){
        uint checkok = n_eth_check_connection(m_card_num);
        connectOk = checkok == 1;
        if(connectOk){
            m_connect_count ++;
        }else{
            m_disconnect_count ++;
        }
        emit sig_onCheckConnect(m_card_num, connectOk, m_disconnect_count, m_connect_count, eth_get_error());
        if(connectOk){
            QThread::msleep(10000);
        }else{
            int count = 0;
            int select = 0;
            do{
                select = acquire_rtc(m_card_num);
                QThread::msleep(1000);
            }while(select != m_card_num && 10 > count++);
            if(select == m_card_num && count < 10){
                QThread::msleep((10 - count) * 1000);
            }
        }
    }
}
